﻿using System;
using System.Collections;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;

namespace Common
{
    public class ZIPFiles
    {
        ArrayList arrayList = new ArrayList();
        string SourceForder = "";
        string SaveForder = "";

        public bool FilesZIP(string[] args)
        {
            SourceForder = args[0].ToString();
            SaveForder = args[1].ToString();

            if (!addZipDataInArrayList(args))
                return false;

            if (!addZipEntry())
                return false;

            //if (!DeleteFolder(SourceForder))
            //    return false;

            return true;
        }

        protected bool addZipEntry()
        {
            ZipOutputStream ZipOut = new ZipOutputStream(File.Create(SaveForder));
            try
            {
                string strPath = string.Empty;
                Crc32 crc = new Crc32();
                ZipOut.SetLevel(6);
                foreach (string item in arrayList)
                {
                    strPath = SourceForder + @"\" + item;
                    FileStream stream = File.OpenRead(strPath);
                    byte[] buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, buffer.Length);
                    ZipEntry entry = new ZipEntry(item);
                    entry.DateTime = DateTime.Now;
                    entry.Size = stream.Length;
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    ZipOut.PutNextEntry(entry);
                    ZipOut.Write(buffer, 0, buffer.Length);
                    //记得要清空缓冲并关闭文件流
                    stream.Flush();
                    stream.Close();
                }
            }
            catch
            {
                ZipOut.Close();
                ZipOut.Dispose();
                return false;
            }
            ZipOut.Finish();
            ZipOut.Close();
            ZipOut.Dispose();
            return true;
        }

        protected bool addZipDataInArrayList(string[] args)
        {
            try
            {
                string strEntryName = string.Empty;
                DirectoryInfo Direcs = new DirectoryInfo(args[0]);

                foreach (FileInfo item in Direcs.GetFiles())
                {
                    //把要压缩的文件加到数组
                    FileStream stream = File.OpenRead(item.FullName);
                    byte[] buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, buffer.Length);
                    strEntryName = item.FullName.Replace(SourceForder + @"\", "");
                    arrayList.Add(strEntryName);
                    //记得要清空缓冲并关闭文件流
                    stream.Flush();
                    stream.Close();
                }

                string[] subargs = new string[2];
                foreach (DirectoryInfo directory in Direcs.GetDirectories())
                {
                    subargs[0] = args[0] + @"\" + directory.Name;
                    subargs[1] = args[1];
                    if (!addZipDataInArrayList(subargs))
                        return false;
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        protected bool DeleteFolder(string dir)
        {
            try
            {
                //if (Directory.Exists(dir)) //如果存在这个文件夹删除之 
                //{
                //    foreach (string d in Directory.GetFileSystemEntries(dir))
                //    {
                //        if (File.Exists(d))
                //            File.Delete(d); //直接删除其中的文件 
                //        else
                //            DeleteFolder(d); //递归删除子文件夹 
                //    }
                //    Directory.Delete(dir); //删除已空文件夹 
                //}
                if (Directory.Exists(dir))
                    Directory.Delete(dir);
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}
